﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Killboard.Models;
using System.Web.Mvc;
using System.Web.UI;

namespace KillParser
{
    public class Pilot
    {
        private bool isNPC = false;

        public bool IsNPC
        {
            get { return isNPC; }
            set { isNPC = value; }
        }

        private bool laidTheFinalBlow = false;

        public bool LaidTheFinalBlow
        {
            get { return laidTheFinalBlow; }
            set { laidTheFinalBlow = value; }
        }

        private int? corpID;

        public int? CorpID
        {
            get { return corpID; }
            set { corpID = value; }
        }

        private int? allianceID;

        public int? AllianceID
        {
            get { return allianceID; }
            set { allianceID = value; }
        }
        
        private int? pilotID;

        public int? PilotID
        {
            get { return pilotID; }
            set { pilotID = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string corpName;

        public string CorpName
        {
            get { return corpName; }
            set { corpName = value; }
        }

        private string alliance;

        public string Alliance
        {
            get { return alliance; }
            set { alliance = value; }
        }


        private string faction;

        public string Faction
        {
            get { return faction; }
            set { faction = value; }
        }

        private string ship;

        public string Ship
        {
            get { return ship; }
            set { ship = value; }
        }

        private string weapon;

        public string Weapon
        {
            get { return weapon; }
            set { weapon = value; }
        }

        private short? shipID;

        public short? ShipID
        {
            get { return shipID; }
            set { shipID = value; }
        }

        private short? weaponID;

        public short? WeaponID
        {
            get { return weaponID; }
            set { weaponID = value; }
        }

        private int damageTakenorDone;

        public int DamageTakenOrDone
        {
            get { return damageTakenorDone; }
            set { damageTakenorDone = value; }
        }

        private float secStatus;

        public float SecStatus
        {
            get { return secStatus; }
            set { secStatus = value; }
        }

        private ekbPilot dbPilot;

        public ekbPilot DbPilot
        {
            get { return dbPilot; }
            set { dbPilot = value; }
        }

    }

    public class StarSystem
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private float secStatus;

        public float SecStatus
        {
            get { return secStatus; }
            set { secStatus = value; }
        }

        private int? solSystemID;

        public int? SolSystemID
        {
            get { return solSystemID; }
            set { solSystemID = value; }
        }
    }

    public class Item
    {
        private string itemName;

        public string ItemName
        {
            get { return itemName; }
            set { itemName = value; }
        }

        private short itemQty;

        public short ItemQty
        {
            get { return itemQty; }
            set { itemQty = value; }
        }

        private bool inCargo;

        public bool InCargo
        {
            get { return inCargo; }
            set { inCargo = value; }
        }

        private bool wasDestroyed;

        public bool WasDestroyed
        {
            get { return wasDestroyed; }
            set { wasDestroyed = value; }
        }

        private invType dbItem;

        public invType DbItem
        {
            get { return dbItem; }
            set { dbItem = value; }
        }

    }

    public static class ekbCalendar
    {
        public static DateTime GetBeginningDate(int week, int year)
        {
            //Find the first Thursday of the given year

            DateTime begOfYear = new DateTime(year, 1, 1);

            while(begOfYear.DayOfWeek != DayOfWeek.Thursday)
            {
                // add one day until we get to the first thursday.
                begOfYear = begOfYear.Add(new TimeSpan(1, 0 ,0 ,0));
            }

            begOfYear = begOfYear.Add(new TimeSpan(week * 7, 0, 0, 0));

            while (begOfYear.DayOfWeek != DayOfWeek.Sunday)
            {
                begOfYear = begOfYear.Subtract(new TimeSpan(1, 0, 0, 0));
            }



            return begOfYear;

        }

        private static int GetCurrentWeek()
        {
            System.Globalization.CultureInfo myCI = new System.Globalization.CultureInfo("en-US");
            int week = myCI.Calendar.GetWeekOfYear(DateTime.Now.ToUniversalTime(), System.Globalization.CalendarWeekRule.FirstFourDayWeek,
            System.DayOfWeek.Sunday);

            return week-1;
        }

        public static DateTime GetBeginningofCurrentWeek()
        {
            int currWeek = GetCurrentWeek();
            return GetBeginningDate(currWeek, DateTime.Now.Year);
        }

    }

    public class BadKillException : Exception
    {
        public BadKillException() : base() { }
        public BadKillException(string message) : base(message) { }
        public BadKillException(string message, Exception innerException) : base(message, innerException) { }
    }

    public class CannotSaveException : Exception
    {
        public CannotSaveException(string message) : base(message) { }
    }

    public class KillExistsException : Exception
    {
        public KillExistsException(DateTime killID) : base(string.Format("A kill with date: {0:MM/dd/yyyy hh:mm:ss} already exists.", killID)){}

    }

    public class TooManyConfigsException : Exception
    {
        public TooManyConfigsException() : base() {}
    }

    public class NoConfigException : Exception
    {
        public NoConfigException() : base() { }
    }

    public class BadPasswordException : Exception
    {
        public BadPasswordException() : base(){}
    }


    public static class MVCHelpers
    {

        public static string Image(this HtmlHelper helper, string name, string path, IDictionary<string, object> attributes)
        {

            StringBuilder sb = new StringBuilder();

            string root = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
            
            if (!root.EndsWith(@"/"))
            {
                root += @"/";
            }

            sb.Append("<img src=\"");
            sb.Append(root);
            sb.Append(path);
            sb.Append("\"");
            sb.Append(" ");

            if (attributes != null)
            {
                foreach (var item in attributes)
                {
                    sb.AppendFormat("{0}=\"{1}\"", item.Key, item.Value);
                }
            }

            sb.Append(" />");

            return sb.ToString();
        }

        public static string WebRoot(this HtmlHelper helper)
        {
            string root = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
            if (!root.EndsWith(@"/"))
            {
                root += @"/";
            }

            return root;
        }
    }

}
